/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Somesh Soni
 */
package com.mxgraph.examples.swing;

import java.sql.Connection;

import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class SNMPFrame extends javax.swing.JFrame {

    String DeviceName;
    String DeviceUID;
    String contactEmailID;
    String sysAdmin;
    String alarmEmail;
    String IPs;
    String snmpPublicString;
    String snmpPrivateString;

    /**
     * Creates new form IPFrame
     */
    public SNMPFrame(String DeviceID, String Name, String contactID, String sysAdm, String alarmemail, String IP, String snmpPublic, String snmpPrivate) {
        setVisible(true);
        DeviceName = Name;
        DeviceUID = DeviceID;
        IPs = IP;
        alarmEmail = alarmemail;
        sysAdmin = sysAdm;
        contactEmailID = contactID;
        snmpPublicString = snmpPublic;
        snmpPrivateString = snmpPrivate;
        if (snmpPublicString.isEmpty()) {
            snmpPublicString = "public";
            snmpPrivateString = "private";
        }

        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PFNMSLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        IPAddressLabel = new javax.swing.JLabel();
        NetMaskLabel = new javax.swing.JLabel();
        ProceedButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        PFNMSLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setAlwaysOnTop(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        PFNMSLabel.setBackground(new java.awt.Color(102, 102, 102));
        PFNMSLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        PFNMSLabel.setText("PacketFence Network Monitoring system");

        jLabel1.setToolTipText("");

        IPAddressLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        IPAddressLabel.setText("Read Only Community String");

        NetMaskLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        NetMaskLabel.setText("Read Write Community String");

        ProceedButton.setText("Finish");
        ProceedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ProceedButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("PacketFenceNMS uses SNMP community string to find out the devices in the defined network.");

        PFNMSLabel1.setBackground(new java.awt.Color(102, 102, 102));
        PFNMSLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        PFNMSLabel1.setText("SNMP Community String");

        jTextField1.setText(snmpPrivateString);

        jTextField2.setText(snmpPublicString);
        jTextField2.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(PFNMSLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(338, 338, 338))
            .addGroup(layout.createSequentialGroup()
                .addGap(97, 97, 97)
                .addComponent(PFNMSLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 534, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addComponent(IPAddressLabel))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(99, 99, 99)
                                .addComponent(NetMaskLabel))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(130, 130, 130)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 480, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(204, 204, 204)
                            .addComponent(jLabel1))))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(ProceedButton)
                .addGap(18, 18, 18)
                .addComponent(CancelButton)
                .addGap(58, 58, 58))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(PFNMSLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PFNMSLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(IPAddressLabel)
                            .addComponent(NetMaskLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ProceedButton)
                    .addComponent(CancelButton))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        // TODO add your handling code here:
        String cmd = evt.getActionCommand();
        if (cmd.equals("Cancel")) {
            dispose();
        }
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void ProceedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ProceedButtonActionPerformed
        // TODO add your handling code here:
    int counter = 0;
   Connection connect = null;
    String myDriver = "com.mysql.jdbc.Driver";
        try{
        connect = DriverManager.getConnection("jdbc:mysql://localhost/nms", "root", "");
        
        //connect = DriverManager.getConnection("jdbc:mysql://localhost/nms", "root"," ");
        Class.forName(myDriver);
        System.out.println("Select");
        String query = "SELECT * FROM `device_information` WHERE value='" + DeviceUID+"'";
        System.out.println(query);
        // create the java statement
        Statement st = connect.createStatement();
System.out.println("stateme");
        // execute the query, and get a java resultset
        ResultSet rs = st.executeQuery(query);
        System.out.println("rs");
        System.out.println("counter");
        counter = rs.getRow();
        
        if(counter!=0){
            System.out.println("update");
            String queryUpdate ="UPDATE device_information SET  Name="+DeviceName+",contactID="+ contactEmailID+",sysadm="+sysAdmin+",alarmemail="+alarmEmail+",IP="+IPs+",snmpPublic="+snmpPublicString+",snmpPrivate="+snmpPrivateString+ "where value="+DeviceUID;
st.executeUpdate(queryUpdate);
        }
        else{
            System.out.println("INSERT INTO device_information VALUES (value="+DeviceUID+", Name="+DeviceName+",contactID="+ contactEmailID+",sysadm="+sysAdmin+",alarmemail="+alarmEmail+",IP="+IPs+"snmpPublic="+snmpPublicString+",snmpPrivate="+snmpPrivateString+ ")");
        String queryUpdate ="INSERT INTO `device_information` (`value`, `Name`, `contactID`, `sysadm`, `alarmemail`, `IP`, `snmpPublic`, `snmpPrivate`, `Serial No.`) VALUES ('"+DeviceUID+"', '"+DeviceName+"','"+ contactEmailID+"','"+sysAdmin+"','"+alarmEmail+"','"+IPs+"','"+snmpPublicString+"','"+snmpPrivateString+ "',NULL)";
st.executeUpdate(queryUpdate);
        }
        // iterate through the java resultset
        
    }
    catch(Exception e

    
        ){System.err.println(e);
    }
    }//GEN-LAST:event_ProceedButtonActionPerformed
/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                

}
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IPFrame.class  

.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 

catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IPFrame.class  

.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 

catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IPFrame.class  

.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 

catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IPFrame.class  

.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //new IPFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JLabel IPAddressLabel;
    private javax.swing.JLabel NetMaskLabel;
    private javax.swing.JLabel PFNMSLabel;
    private javax.swing.JLabel PFNMSLabel1;
    private javax.swing.JButton ProceedButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
